<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      :props="{label:'name'}"
      default-expand-all
      show-checkbox
      node-key="id"
      :check-strictly="true"
    />
    <div>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="closeDialog">取消</el-button>
    </div>

  </div>

</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeList } from '@/utils/index'
import { getRoleDetail, assignPrem } from '@/api/setting'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    closeDialog() {
      this.$emit('success')
    },
    async hSubmit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      const res = assignPrem({ id: this.roleId, permIds: permIds })
      console.log(res, 'assignPrem')
      this.$emit('success')
    },
    async loadPermissionList() {
      const res = await getPermissionList()
      const perIds = await getRoleDetail(this.roleId)
      console.log(res, 'getPermissionList')
      console.log(perIds, 'getRoleDetail')
      this.list = tranListToTreeList(res.data)
      this.$refs.tree.setCheckedKeys(perIds.data.permIds)
    }
  }
}
</script>
